﻿using IronPython.Hosting;
using jlynet.core.notebook.myform;
using Microsoft.Scripting.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace jlynet.core.notebook
{
    static class Program
    {
        [DllImport("user32.dll")]
        private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
        [DllImport("user32.dll")]
        private static extern bool FlashWindowEx(int pfwi);

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool runone;
            System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);
            if (runone)
            {
                //资源文件，也就是Resources.resx
                //Properties.Resources.资源名

                //设置Python运行环境
                ScriptEngine engine = Python.CreateEngine();
                ScriptScope scope = engine.CreateScope();
                ScriptSource source = engine.CreateScriptSourceFromString(
                    @"import sys" + "\n" +
                    @"sys.path.append("".\scripts\ironPythonLib.zip"")" + "\n" +
                    @"sys.path.append("".\scripts"")" + "\n"
                    );
                source.Execute(scope);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                LandingForm l = new LandingForm();
                if (l.ShowDialog() == DialogResult.OK)
                {
                    MiniPasswordBoxForm form = new MiniPasswordBoxForm();
                    form.Text = "迷你密码箱--[" + l.User_FilePath + "]";
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    dic["User_FilePath"] = l.User_FilePath;
                    form.Tag = dic;
                    int hdc = form.Handle.ToInt32(); // write to ...
                    System.Windows.Forms.Application.EnableVisualStyles(); //这两行实现   XP   可视风格   
                    System.Windows.Forms.Application.DoEvents(); //这两行实现   XP   可视风格
                    Application.Run(form);
                    run.ReleaseMutex();
                    IntPtr a = new IntPtr(hdc);
                }
                //Application.Run(new MiniPasswordBoxForm());
                //Application.Run(new Form1());
            }else {
                MessageBox.Show(String.Format("已经运行了一个{0}程序！", Program.AssemblyProduct));
                //IntPtr hdc = new IntPtr(1312810); // read from...
                //bool flash = FlashWindow(hdc, true);
            }
        }

        public static string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

    }
}
